package com.fly.caihong.view;

/**
 * 创建时间：2018/5/17 0017 12:01
 * 创建人：fly
 * 功能描述：
 */

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.fly.caihong.R;


/**
 * 自定义简单Toolbar
 */
public class SimpleToolbar extends Toolbar {

    private ImageButton mBtnBack;
    private TextView mTxtMiddleTitle;
    private TextView mTxtRightTitle;
    private TextView mTxtSecondRightTitle;
    private View mViewContainer, mViewDivider;

    public SimpleToolbar(Context context) {
        this(context, null);
    }

    public SimpleToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SimpleToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setId(R.id.tb_toolbar);
        LayoutInflater.from(context).inflate(R.layout.layout_toolbar, this, true);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mBtnBack = findViewById(R.id.btn_back);
        mTxtMiddleTitle = findViewById(R.id.tv_main_title);
        mTxtRightTitle = findViewById(R.id.tv_right_title);
        mTxtSecondRightTitle = findViewById(R.id.tv_second_right_title);
        mViewDivider = findViewById(R.id.view_divider);
        mViewContainer = findViewById(R.id.rl_container);
    }

    /**
     * 设置中间title的内容
     */
    public void setMainTitle(String text) {
        this.setTitle(" ");
        mTxtMiddleTitle.setVisibility(View.VISIBLE);
        mTxtMiddleTitle.setText(text);
    }

    /**
     * 设置中间title的内容
     */
    public void setMainTitle(@StringRes int stringId) {
        this.setTitle(" ");
        mTxtMiddleTitle.setVisibility(View.VISIBLE);
        mTxtMiddleTitle.setText(stringId);
    }

    /**
     * 设置中间title的内容文字的颜色
     *
     * @param color
     */
    public void setMainTitleColor(int color) {
        mTxtMiddleTitle.setTextColor(color);
    }


    /**
     * 设置左侧返回图标
     *
     * @param resId
     */
    public void setLeftBackResource(int resId) {
        mBtnBack.setImageResource(resId);
    }

    /**
     * 设置title左边点击事件
     *
     * @param onClickListener
     */
    public void setOnLeftClickListener(OnClickListener onClickListener) {
        mBtnBack.setOnClickListener(onClickListener);
    }

    /**
     * 设置title右边文字
     *
     * @param text
     */
    public void setRightTitleText(String text) {
        mTxtRightTitle.setVisibility(View.VISIBLE);
        mTxtRightTitle.setText(text);
    }

    /**
     * 设置title右边文字
     *
     * @param text
     */
    public void setSecondRightTitleText(String text) {
        mTxtSecondRightTitle.setVisibility(View.VISIBLE);
        mTxtSecondRightTitle.setText(text);
    }

    /**
     * 设置title右边文字
     *
     * @param stringId
     */
    public void setRightTitleText(@StringRes int stringId) {
        mTxtRightTitle.setVisibility(View.VISIBLE);
        mTxtRightTitle.setText(stringId);
    }

    /**
     * 设置title右边文字
     *
     * @param stringId
     */
    public void setSecondRightTitleText(@StringRes int stringId) {
        mTxtSecondRightTitle.setVisibility(View.VISIBLE);
        mTxtSecondRightTitle.setText(stringId);
    }

    /**
     * 设置title右边文字颜色
     *
     * @param color
     */
    public void setRightTitleColor(int color) {
        mTxtRightTitle.setTextColor(color);
    }

    /**
     * 设置title右边文字颜色
     *
     * @param size
     */
    public void setRightTitleSize(float size) {
        mTxtRightTitle.setTextSize(size);
    }

    /**
     * 设置title右边文字颜色
     *
     * @param color
     */
    public void setSecondRightTitleColor(int color) {
        mTxtSecondRightTitle.setTextColor(color);
    }


    /**
     * 设置title右边图标
     *
     * @param res
     */
    public void setRightTitleDrawable(int res) {
        Drawable dwRight = ContextCompat.getDrawable(getContext(), res);
        dwRight.setBounds(0, 0, dwRight.getMinimumWidth(), dwRight.getMinimumHeight());
        mTxtRightTitle.setCompoundDrawables(null, null, dwRight, null);
    }

    /**
     * 设置title右边图标
     *
     * @param res
     */
    public void setSecondRightTitleDrawable(int res) {
        Drawable dwRight = ContextCompat.getDrawable(getContext(), res);
        dwRight.setBounds(0, 0, dwRight.getMinimumWidth(), dwRight.getMinimumHeight());
        mTxtSecondRightTitle.setCompoundDrawables(null, null, dwRight, null);
    }

    /**
     * 设置title右边点击事件
     *
     * @param onClickListener
     */
    public void setOnRightClickListener(OnClickListener onClickListener) {
        mTxtRightTitle.setOnClickListener(onClickListener);
    }

    /**
     * 设置title右边点击事件
     *
     * @param onClickListener
     */
    public void setOnSecondRightClickListener(OnClickListener onClickListener) {
        mTxtSecondRightTitle.setOnClickListener(onClickListener);
    }

    /**
     * 设置下滑线显示或隐藏
     *
     * @param isVisible
     */
    public void setBottomDividerVisible(boolean isVisible) {
        mViewDivider.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    /**
     * 获取右侧操作标题
     *
     * @return
     */
    public TextView getTxtRightTitle() {
        return mTxtRightTitle;
    }

    /**
     * 设置背景颜色
     *
     * @param color
     */
    public void setLayoutBackgroundColor(int color) {
        mViewContainer.setBackgroundColor(color);
    }

    /**
     * 清除右侧操作背景
     */
    public void clearRightEditTextBackgournd() {
        mTxtRightTitle.setBackground(null);
    }

    /**
     * 清除右二侧操作背景
     */
    public void clearSecondRightEditTextBackgournd() {
        mTxtSecondRightTitle.setBackground(null);
    }

    /**
     * 设置title alpha值
     *
     * @param alpha
     */
    public void setMainTitleAlpha(float alpha) {
        mTxtMiddleTitle.setAlpha(alpha);
    }

}
